package com.uit.httt04.duongvanlam.facedetection.utils;

public class Fibonacci extends Object
{
	private double[] Values;
	private int amountOfElements;

	public Fibonacci(int amountOfElements)
	{
		this.amountOfElements = amountOfElements;
		this.Values = new double[this.amountOfElements];

		// 1,1,2,3,5,8,13,21,34,55,89 ...
		double a1 = 55;
		double a2 = 89;

		for (int i = this.amountOfElements - 1; i >= 0; i--)
		{
			if (i == (this.amountOfElements - 1) || i == (this.amountOfElements - 2))
			{
				if (i == (this.amountOfElements - 1))
					this.Values[i] = a1;
				if (i == (this.amountOfElements - 2))
					this.Values[i] = a2;
			}
			else
			{
				this.Values[i] = this.Values[i + 1] + this.Values[i + 2];
			}
		}
	}

	public double getValue(int rank)
	{
		try
		{
			if (this.Values != null && rank < this.amountOfElements)
			{
				return this.Values[rank];
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return 0.0;
	}

	@Override
	public void finalize() throws Throwable
	{
		Values = null;
		super.finalize();
	}
}
